import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Job from '../views/Job'
import My from '../views/My'
import VuexTest from '../views/VuexTest/index'
import Detail from '../views/Detail'
import Wine from '../views/Wine'
import Main from '../views/Main'
import Reg from '../views/Reg'
import Login from '../views/Login'
import store from '../store/index'
import BScroll from '../views/BScroll'
import JoinList from '../views/JoinList'
Vue.use(VueRouter)
let isLogin = false //模拟登录状态
const router = new VueRouter({
  routes:[
    {
      path:'/',
      component:Main,
      redirect:'/home',
      children:[
        {
          path:'home',
          component:Home
        },
        {
          path:'job',
          component:Job
        },
        {
          path:'my',
          component:My,
          beforeEnter(to,from,next){ //路由独享守卫
            if(store.state.user.userInfo){
              next()
            }else{
              next({
                path:'/login',
                query:{
                  target:to.path
                }
              })
            }
          }
        },
      ]
    },
    {
      path:'/detail',
      component:Detail
    },
    {
      path:'/join/list',
      component:JoinList
    },
    {
      path:'/reg',
      component:Reg
    },
    {
      path:'/login',
      component:Login
    },
    {
      path:'/wine',
      component:Wine
    },
    {
      path:'/bs',
      component:BScroll
      // () => import('../views/Cart.vue')

    },
    {
      path:'/vuex/test',
      component:VuexTest
    }
  ]
})


// router.beforeEach((to,from,next)=>{ //全局前置守卫
//   // to 记录了想要访问的目标路由信息
//   // from 记录了来源的路由信息
//   // next 是否放行的方法
//   // console.log(to,from);
//   // next()
//   if(to.path.indexOf('/my')!=-1){
//     if(isLogin){
//       next()
//     }else{
//       next('/login')
//     }
//   }else{  
//     next()
//   }
// })

export default router  //切记必须抛出